using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using System.Threading;

namespace Alex_s_Dream_The_Toy_World
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TheToyWorldGame : FrameworkCore
    {
        #region Feilds

        /// <summary>
        /// Input poller so we can query multi-key combos, like CTRL + Enter
        /// </summary>
        private InputPollingHandler inputs;

        #endregion
        /// <summary>
        /// Constructor.
        /// </summary>
        public TheToyWorldGame()
        {
            Content.RootDirectory = "Content";

            //  Set to window title name
            Window.Title = "The Toy World Game";

            this.IsMouseVisible = Constants.MouseDefaultVisible;
        }

        /// <summary>
        /// Initializes the current sample game instance.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // Set up a window with maximum MSAA
            GraphicsSettings settings = new GraphicsSettings();
            settings.BackBufferWidth = this.Settings.Resolution.X;
            settings.BackBufferHeight = this.Settings.Resolution.Y;
            settings.EnableVSync = this.Settings.IsVerticalSyncronized;
            settings.EnableMSAA = this.Settings.IsMultiSampling;
            settings.EnableFullScreen = this.Settings.IsFullscreen;
            settings.EnableShadowMapping = this.Settings.IsShadowMapping;
            settings.GraphicsLevel = this.Settings.GraphicsLevel;

            this.IsFixedTimeStep = this.Settings.IsFixedTimeStep;

            // Uncomment this line for PIX debugging
            //settings.EnableMSAA = false;

            Graphics.ApplySettings(settings);

            MsgSetPhysicsTimeStep msgSetPhysStep = ObjectPool.Aquire<MsgSetPhysicsTimeStep>();
            msgSetPhysStep.TimeStep = this.Settings.PhysicsStepsPerSecond;
            SendInterfaceMessage(msgSetPhysStep, InterfaceType.Physics);

            InitInputListeners();
        }

        /// <summary>
        /// Add input listeners for input polling
        /// </summary>
        private void InitInputListeners()
        {
            this.inputs = new InputPollingHandler(this);

            this.inputs.AddInputListener(Keys.Enter);
            this.inputs.AddInputListener(Keys.LeftControl);
            this.inputs.AddInputListener(Keys.RightControl);
            this.inputs.AddInputListener(Keys.LeftAlt);
            this.inputs.AddInputListener(Keys.RightAlt);

            this.inputs.SetLockable(Keys.LeftAlt, true);
            this.inputs.SetLockable(Keys.RightAlt, true);
        }

        #region Entry Point


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            using (TheToyWorldGame game = new TheToyWorldGame())
            {
                game.Run();
            }
        }


        #endregion
    }
}
